package com.mystore.manage.controller;

import com.mystore.common.jsonx.JSONEntity;
import com.mystore.common.paging.StylePage;
import com.mystore.framework.pojo.Role;
import com.mystore.framework.service.impl.IRoleServiceImpl;
import com.mystore.framework.service.impl.IUriServiceImpl;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * 角色表(Role)表控制层
 */
@Controller
@RequestMapping("manage/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private IRoleServiceImpl iRoleServiceImpl;
    @Resource
    private IUriServiceImpl iUriServiceImpl;
    @GetMapping("/page.do")
    public String page(@RequestParam(defaultValue="")String data,Model model, @RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size,String result) {
        IPage<Role> rolePage = iRoleServiceImpl.selectPageByData(data,new Page<>(current, size));
        model.addAttribute("roles", new StylePage<>(rolePage));
        model.addAttribute("result",result);
        model.addAttribute("data",data);
        return "role_page";
    }

    @GetMapping("/insert.do")
    public String insert(Model model) {
        model.addAttribute("mode", "insert");
        model.addAttribute("allUri",iUriServiceImpl.selectAll());
        return "role_info";
    }

    @GetMapping("/update.do")
    public String update(Model model, int roleId) {
        Role role=iRoleServiceImpl.selectById(roleId);
        model.addAttribute("role", role);
        model.addAttribute("mode", "update");
        model.addAttribute("allUri",iUriServiceImpl.selectAll());
        model.addAttribute("uriSet",iUriServiceImpl.selectUrisByIds(role.getUris()));
        return "role_info";
    }
    @GetMapping("/selectUris.do")
    @ResponseBody
    public JSONEntity selectUris(int roleId){
        Role role=iRoleServiceImpl.selectById(roleId);
        if (role==null)
            return JSONEntity.error(null);
        else
            return JSONEntity.ok(iUriServiceImpl.selectUrisByIds(role.getUris()));
    }
    @PostMapping("/save.do")
    public String save(RedirectAttributes model, Role role, String mode) {
        if (mode.equals("insert"))
            model.addAttribute("result", iRoleServiceImpl.insertOne(role)>0);
        else
            model.addAttribute("result", iRoleServiceImpl.updateOne(role));
        model.addAttribute("mode", mode);
        return "redirect:/manage/role/page.do";
    }

    @GetMapping("/delete.do")
    public String delete(RedirectAttributes model, int roleId) {
        model.addAttribute("result", iRoleServiceImpl.deleteById(roleId));
        return "redirect:/manage/role/page.do";
    }

}

